package com.liu.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.seckill.exception.GlobalException;
import com.liu.seckill.mapper.UserMapper;
import com.liu.seckill.pojo.User;
import com.liu.seckill.service.IUserService;
import com.liu.seckill.util.*;
import com.liu.seckill.vo.LoginVo;
import com.liu.seckill.vo.RespBean;
import com.liu.seckill.vo.RespBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName UserServiceImpl
 * @Author ：name
 * @Date ：2022-02-14 17:06
 * @Description：
 * @Version: 1.0
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录
     * @param loginVo
     * @return
     */
    @Override
    public RespBean login(HttpServletRequest request, HttpServletResponse
            response, LoginVo loginVo) {

        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        //根据手机号获取用户
      User user = userMapper.selectById(mobile);
       if (null==user){
         return RespBean.error(RespBeanEnum.LOGINVO_ERROR);
       }
       //校验密码
        String pdmd=MD5Util.formPassToDBPass(password,user.getSlat());
        if(!pdmd.equals(user.getPassword())){

         throw new GlobalException(RespBeanEnum.ERROR);

       }
        String ticket = UUIDUtil.uuid();
        log.info("cookie:{}",ticket);
        redisTemplate.opsForValue().set("user:"+ticket, JsonUtil.object2JsonStr(user));
        CookieUtil.setCookie(request,response,"userTicket",ticket);
        return RespBean.success(ticket);

    }
    /**
     * 根据cookie获取用户
     * @param userTicket
     * @param request
     * @param response
     * @return
     */
    @Override
    public User getByUserTicket(String userTicket, HttpServletRequest request, HttpServletResponse response) {
      if (StringUtils.isEmpty(userTicket)){
          return null;
      }
      String userJson=(String) redisTemplate.opsForValue().get("user:"+userTicket);
        User user = JsonUtil.jsonStr2Object(userJson, User.class);
        if (null!=user){
            CookieUtil.setCookie(request,response,"userTicket",userTicket);
        }
        return user;
    }

    @Override
    public RespBean updatePassword(String userTicket, Long id, String password) {
      User user=userMapper.selectById(id);
      if (user==null){
          throw new GlobalException(RespBeanEnum.MOBILE_NOT_EXIST);
      }
        user.setPassword(MD5Util.inputPassToDbPass(password,user.getSlat()));
        int result = userMapper.updateById(user);
          if (1 == result) {
           //删除Redis
           redisTemplate.delete("user:" + userTicket);
           return RespBean.success();
          }
          return RespBean.error(RespBeanEnum.PASSWORD_UPDATE_FAIL);
            }
}
